﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-24 14:24:22
// 负责人：liucx
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
    #region 快递费用模板 数据访问层
    /// <summary>
    ///快递费用模板 数据访问层
    /// </summary>	
    internal partial class ExpressFeeTpl : IExpressFeeTpl
    {

        //连接字符串
        public virtual string ConnectionStringKey
        {
            get
            {
                return ConstantUtility.CGTConnectionString;
            }
        }
        #region GetMaxID



        public int GetMaxID()
        {
            int nReturn = 0;
            string szSql = @"SELECT ISNULL(MAX(ExpressFeeTplID),0)+1
                               FROM [ExpressFeeTpl]";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(szSql);
            try
            {
                object o = db.ExecuteScalar(dbc);
                if (o != null)
                {
                    nReturn = Convert.ToInt32(o);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nReturn;
        }



        #endregion

        #region Basic CRUD functions
        private ExpressFeeTplInfo GetByReader(IDataReader dr)
        {
            ExpressFeeTplInfo info = new ExpressFeeTplInfo();
            object obj;
            obj = dr["ExpressFeeTplID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ExpressFeeTplID = Convert.ToInt32(obj);
            }
            obj = dr["ExpressCompanyID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ExpressCompanyID = Convert.ToInt32(obj);
            }
            obj = dr["TemplateName"];
            if (obj != null && obj != DBNull.Value)
            {
                info.TemplateName = obj.ToString();
            }
            obj = dr["BizTypeID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.BizTypeID = Convert.ToInt32(obj);
            }
            obj = dr["ExpressFee"];
            if (obj != null && obj != DBNull.Value)
            {
                info.ExpressFee = Convert.ToDecimal(obj);
            }
            obj = dr["Remark"];
            if (obj != null && obj != DBNull.Value)
            {
                info.Remark = obj.ToString();
            }
            obj = dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj = dr["UpdatedDate"];
            if (obj != null && obj != DBNull.Value)
            {
                info.UpdatedDate = Convert.ToDateTime(obj);
            }
            obj = dr["CreatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.CreatedUserID = Convert.ToInt32(obj);
            }
            obj = dr["UpdatedUserID"];
            if (obj != null && obj != DBNull.Value)
            {
                info.UpdatedUserID = Convert.ToInt32(obj);
            }
            obj = dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {
                info.IsActive = Convert.ToInt32(obj);
            }
            return info;
        }
        public int Insert(ExpressFeeTplInfo info)
        {


            string strSql = @"INSERT INTO ExpressFeeTpl			
			(			
			[ExpressCompanyID],
			[TemplateName],
			[BizTypeID],
			[ExpressFee],
			[Remark],
			[CreatedDate],
			[UpdatedDate],
			[CreatedUserID],
			[UpdatedUserID],
			[IsActive]
			)
			 VALUES
			(			
			@ExpressCompanyID,
			@TemplateName,
			@BizTypeID,
			@ExpressFee,
			@Remark,
			@CreatedDate,
			@UpdatedDate,
			@CreatedUserID,
			@UpdatedUserID,
			@IsActive
			)
			 SET @ExpressFeeTplID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@ExpressFeeTplID", DbType.Int32, sizeof(Int32));
            db.AddInParameter(dbc, "@ExpressCompanyID", DbType.Int32, info.ExpressCompanyID);
            db.AddInParameter(dbc, "@TemplateName", DbType.String, info.TemplateName);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@ExpressFee", DbType.Decimal, info.ExpressFee);
            db.AddInParameter(dbc, "@Remark", DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreatedDate", DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate", DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID", DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID", DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive", DbType.Int32, info.IsActive);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
            return (int)db.GetParameterValue(dbc, "ExpressFeeTplID");

        }


        public void Update(ExpressFeeTplInfo info)
        {
            StringBuilder sbSql = new StringBuilder("UPDATE ExpressFeeTpl SET ");
            sbSql.AppendLine("[ExpressCompanyID] = @ExpressCompanyID,");

            sbSql.AppendLine("[TemplateName] = @TemplateName,");

            sbSql.AppendLine("[BizTypeID] = @BizTypeID,");

            sbSql.AppendLine("[ExpressFee] = @ExpressFee,");

            sbSql.AppendLine("[Remark] = @Remark,");

            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");

            sbSql.AppendLine("[UpdatedDate] = @UpdatedDate,");

            sbSql.AppendLine("[CreatedUserID] = @CreatedUserID,");

            sbSql.AppendLine("[UpdatedUserID] = @UpdatedUserID,");

            sbSql.AppendLine("[IsActive] = @IsActive");

            sbSql.Append(" where  [ExpressFeeTplID] = @ExpressFeeTplID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@ExpressFeeTplID", DbType.Int32, info.ExpressFeeTplID);
            db.AddInParameter(dbc, "@ExpressCompanyID", DbType.Int32, info.ExpressCompanyID);
            db.AddInParameter(dbc, "@TemplateName", DbType.String, info.TemplateName);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@ExpressFee", DbType.Decimal, info.ExpressFee);
            db.AddInParameter(dbc, "@Remark", DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreatedDate", DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdatedDate", DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@CreatedUserID", DbType.Int32, info.CreatedUserID);
            db.AddInParameter(dbc, "@UpdatedUserID", DbType.Int32, info.UpdatedUserID);
            db.AddInParameter(dbc, "@IsActive", DbType.Int32, info.IsActive);


            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(int expressFeeTplID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [ExpressFeeTpl]  ");
            strSql.Append("WHERE [ExpressFeeTplID]=@ExpressFeeTplID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@ExpressFeeTplID", DbType.Int32, expressFeeTplID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }


        }

        public IList<ExpressFeeTplInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<ExpressFeeTplInfo> list = new List<ExpressFeeTplInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "ExpressFeeTpl");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    ExpressFeeTplInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }


        public ExpressFeeTplInfo GetByID(int expressFeeTplID)
        {
            Model.ExpressFeeTplInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [ExpressFeeTpl] ");
            strSql.Append("WHERE [ExpressFeeTplID]=@ExpressFeeTplID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@ExpressFeeTplID", DbType.Int32, expressFeeTplID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }


        #endregion
    }
    #endregion
}